package com.cg.big_event.controller;
import com.cg.big_event.entity.Article;
import com.cg.big_event.entity.PageBean;
import com.cg.big_event.entity.Result;
import com.cg.big_event.service.ArticleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@RequestMapping("/article")
public class AriclelController {
    @Resource
    private ArticleService articleService;
    @PostMapping
    public Result add(@RequestBody  @Validated(Article.Add.class) Article article) {
        articleService.add(article);
        return Result.success();
    }
    @GetMapping
    public Result<PageBean<Article>> list( Integer pageNum,
                                           Integer pageSize,
                                          @RequestParam(required = false) Integer categoryId,
                                          @RequestParam(required = false) String state)
    {
       PageBean<Article> pb= articleService.list(pageNum,pageSize,categoryId,state);
       return Result.success(pb);
    }
    @GetMapping("/detail")
    public Result<Article> detail(Integer id)
    {
        Article article = articleService.detail(id);
        if (article==null)
            return Result.error("文章不存在");
        return Result.success(article);
    }
    @DeleteMapping
    public Result delete(Integer id)
    {
        articleService.delete(id);
        return Result.success();
    }
    @PutMapping
    public Result update(@RequestBody  @Validated({Article.Update.class}) Article article)
    {
        articleService.update(article);
        return Result.success();
    }

}
